import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { Button, Message, ButtonGroup,Loading } from 'tinper-bee';
import mirror, { connect, actions } from "mirrorx";
import model from "./model";

mirror.model(model);
const propTypes = {
    iconType: PropTypes.string,//图标的className，必填
};

const defaultProps = {

};


class Print extends Component {
    constructor(props) {
        super(props);
        this.state = {
        };
    }
    async componentDidMount() { 
        
    }

    ButtonPrint = () => {
        let { authCode,baseUrl,selectData } = this.props;
        let selectedIds = [];
        selectData.map(item => {
            selectedIds.push(item.id);
        })
        if(authCode == "ExpenseConfirmation"|| authCode == "ExpenseClaim"){
            let status = false
            selectData.forEach(function(item){ 
                if(item.state != 3&&item.organizationId == '0001A110000000001TUQ'){
                    status = true
                }
            });
            if(status){
                Message.create({ content:  "单据未审批不可打印", color: 'warning', duration: 10 });
                return;
            }
        }
        
        selectedIds.join(",");
        let stateparam = {
            sysCode: "yddDisabled",
            search_AUTH_APPCODE: authCode
        }
        let tmpParam = {
            menuCode: authCode,
            search_AUTH_APPCODE: authCode
        }
        actions.Print.getStatus({param:stateparam,callback:(res) => {
            actions.Print.getPrintTmpl({param:tmpParam,callback:(obj) => {
                let printcode = obj.printTmpl[0].code;
                window.open(`/occ-cmpt/cmpt/print/preview?tenantId=${obj.tenantId}&printcode=${printcode}&serverUrl=${baseUrl}/dataForPrint&params=%7B"id"%3A"${selectedIds}"%7D&sendType=post`)
            }});
        }})
    }

    render() {
        let {showLoading} = this.props;
        return (
            <>
                <Button
                    shape='border' 
                    onClick={this.ButtonPrint} 
                    size='sm'>
                    打印
                </Button>
                <Loading show={showLoading} />
            </>
        )
    }
}
Print.propTypes = propTypes;
Print.defaultProps = defaultProps;

export const PrintButton = connect(state => state.Print, null)(Print);
